package org.csc.sss.core.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.csc.sss.core.annontation.ResultStrengthen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 统一处理结果返回
 */
@Component
@Aspect
public class ResultAspect {

    private static final Logger log = LoggerFactory.getLogger(ResultAspect.class);

    @Pointcut("@annotation(org.csc.sss.core.annontation.ResultStrengthen)")
    public void annotationPointcut() {
    }

    @Around("annotationPointcut()")
    public <T> Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 初始化参数

        // 获得当前访问的class
        Class<?> className = joinPoint.getTarget().getClass();
        // 获得访问的方法名
        String methodName = joinPoint.getSignature().getName();
        // 得到方法的参数的类型
        Class<?>[] argClass = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
        // 得到方法的参数的类型
        String[] parameterNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
        // 获取参数
        Object[] args = joinPoint.getArgs();

        try {
            // 得到访问的方法对象
            Method method = className.getMethod(methodName, argClass);
            method.setAccessible(true);
            // 判断是否存在@ExtDataSource注解
            if (method.isAnnotationPresent(ResultStrengthen.class)) {
                ResultStrengthen annotation = method.getAnnotation(ResultStrengthen.class);
                // 取出注解中的数据源名
            }
        } catch (Exception e) {
            throw new RuntimeException("动态设置属性报错", e);
        }

        Object obj = joinPoint.proceed();
        log.info("class 名----------------- {}", joinPoint.proceed().getClass().toString());
//        try {
//            if (obj instanceof PageUtils) {
//                return pageUtilsStrengthen((PageUtils) joinPoint.proceed());
//            } else if (obj instanceof List) {
//                return listStrengthen((List) joinPoint.proceed());
//            } else {
//                return objStrengthen(joinPoint.proceed());
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("返回类型出错", e);
//        }
        return obj;
    }

//
//    private static PageUtils pageUtilsStrengthen(PageUtils pageUtils) {
//        if (null != pageUtils && CollectionUtils.isNotEmpty(pageUtils.getList())) {
//            pageUtils.getList().forEach(p -> ReflectUtils.handleModel(p));
//        }
//
//        return pageUtils;
//    }
//
//    private static List listStrengthen(List list) {
//        if (CollectionUtils.isNotEmpty(list) && checkModelType(list.get(0))) {
//            list.forEach(l -> ReflectUtils.handleModel(l));
//        }
//
//        return list;
//    }
//
//    private static Object objStrengthen(Object obj) {
//        if (null != obj && checkModelType(obj)) {
//            ReflectUtils.handleModel(obj);
//        }
//
//        return obj;
//    }

    private static boolean checkModelType(Object obj) {
        if (obj instanceof String) {
            return false;
        }
        if (obj instanceof Integer) {
            return false;
        }
        if (obj instanceof Double) {
            return false;
        }
        if (obj instanceof Float) {
            return false;
        }
        if (obj instanceof Byte) {
            return false;
        }
        if (obj instanceof Short) {
            return false;
        }
        if (obj instanceof Long) {
            return false;
        }
        if (obj instanceof Boolean) {
            return false;
        }

        return true;
    }

}

